// pages/recipe/recipeDetail/detail.js
import echarts from '../../../ec-canvas/echarts'
const app = getApp();
var recipeChart = [];


function initChart(canvas, width, height) {
  const chart = echarts.init(canvas, null, {
    width: width,
    height: height
  });
  canvas.setChart(chart);

  var option = {
    color: ["#36648b", "#2eaa8e", "#ffa500"],
    title: {
      text: '食物成分分析',
      left: 'center',
      textStyle: {
        fontWeight: 'bold',
      },
      subtext: '总热量 ' + recipeChart.calorie + ' 千卡'
    },
    tooltip: {
      trigger: 'item',
      formatter: "{a}\n{b}: {c} ({d}%)"
    },
    legend: {
      orient: 'vertical',
      x: 'left',
      data: ['碳水化合物', '脂肪', '蛋白质'],
      textStyle: {
        fontSize: '12',
      }
    },

    series: [
      {
        name: '主要成分',
        type: 'pie',
        radius: '50%',
        center: ['50%', '60%'],
        label: {
          normal: {
            show: true,
            position: 'outside',
            formatter: '{b}',
            fontSize: 13
          },
          emphasis: {
            show: true,
            textStyle: {
              fontSize: '13'
            }
          }
        },
        data: [
          { value: recipeChart.carbohydrate, name: "碳水化合物" },
          { value: recipeChart.fat, name: "脂肪" },
          { value: recipeChart.protein, name: "蛋白质" }
        ]
      }
    ]
  }
  chart.setOption(option);
  return chart;
}

Page({
  data: {
    ec: {
      onInit: initChart
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getMenuByRecipeID(options.rid);
  },

  getMenuByRecipeID(rid) {
    let that = this;
    let urlBase = app.globalData.urlBase;

    wx.request({
      url: urlBase + '/wx/getRecipeDetail',
      method: 'get',
      data: {
        rid: rid
      },
      header: {
        'content-type': 'application/json'
      },
      success(res) {
        let menuInfo = res.data;
        menuInfo.recipe.CookingStyle = menuInfo.cookStyle;
        that.setData({
          repWithImg: menuInfo.recipe,
          recipeInfo: menuInfo.recipeInfo
        })
      }
    });

    wx.request({
      url: urlBase + '/openData/recipeConstituentInJson',
      method: 'POST',
      data: {
        repId: rid
      },
      success(res) {
        // console.log(res.data);
        recipeChart = res.data;
      }
    });
  },
})

